package com.heima.controller;

import com.heima.common.Result;
import com.heima.domain.dto.VmTypeDto;
import com.heima.domain.po.VmType;
import com.heima.service.VmTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vm-service/vmType")
@Api(tags = "设备管理")
public class VmTypeController {

    @Autowired
    private VmTypeService vmTypeService;

    @GetMapping("/search")
    @ApiOperation("设备分页搜索")
    public Result getByPage(@RequestParam(required = false) Integer pageIndex,
                            @RequestParam(required = false) Integer pageSize,
                            @RequestParam(required = false) String name){
        Result result = vmTypeService.getByPage(pageIndex,pageSize,name);
        return result;
    }

    @PostMapping
    @ApiOperation("新增设备")
    public Boolean addVmType(@RequestBody VmTypeDto dto){
        Boolean flag = vmTypeService.addVmType(dto);
        return flag;
    }

    @PutMapping("/{typeId}")
    @ApiOperation("修改设备")
    public Boolean updateType(@PathVariable Integer typeId,@RequestBody VmTypeDto dto){
        Boolean flag = vmTypeService.updateVmType(typeId,dto);
        return flag;
    }

    @DeleteMapping("/{typeId}")
    @ApiOperation("删除设备")
    public Boolean delate(@PathVariable Integer typeId){
        try {
            vmTypeService.lambdaUpdate().eq(typeId != null, VmType::getTypeId,typeId).remove();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
